package com.j180.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

@RestController
public class UploadController {

    /**
     * CommonsMultipartFile 就是你得到的文件对象，
     * CommonsMultipartFile 必须使用@RequestParam(名称)来指定上传的文件
     * 名称要于表单中 tyle=file 的name的值相同
     *
     * @param
     * @return
     */
    @PostMapping("/upload")
    public String upload(HttpServletRequest request,
                         @RequestParam("username") String username,
                         @RequestParam("photos") CommonsMultipartFile file){
       // System.out.println(username);
       // System.out.println(file);
        //得到原始文件的名字 作用： 拆后缀
        String  org_fiuleName = file.getOriginalFilename();
        String ext = org_fiuleName.substring(org_fiuleName.lastIndexOf("."));
        //创建新的名字
        String newFileName=LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))+ext;
       //获得全局对象
        ServletContext servletContext = request.getServletContext();
        //得到需要参方图片的地方
       String save_path = servletContext.getRealPath("images");
        File save_File = new File(save_path);
        if(!save_File.exists()){
            save_File.mkdir();
        }
        String saveFilePath = save_path+File.separator+newFileName;
        try {
            //上传文件
            file.transferTo(new File(saveFilePath));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "success";
    }

     //多文件上传
    @RequestMapping("/multiupLoad")
    public String  uploadFiles(HttpServletRequest request,
                                  @RequestParam("photos") CommonsMultipartFile[] fileList){

        for (CommonsMultipartFile file : fileList) {

             //防止重名
          /*  try {
               // Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
            String  org_fiuleName = file.getOriginalFilename();
            String ext = org_fiuleName.substring(org_fiuleName.lastIndexOf("."));
            //创建新的名字
            //String newFileName=LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))+ext;
            String newFileName = UUID.randomUUID().toString()+ext;
            //获得全局对象
            ServletContext servletContext = request.getServletContext();
            //得到需要参方图片的地方
            String save_path = servletContext.getRealPath("images");
            File save_File = new File(save_path);
            if(!save_File.exists()){
                save_File.mkdir();
            }
            String saveFilePath = save_path+File.separator+newFileName;
            try {
                //上传文件
                file.transferTo(new File(saveFilePath));
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


        return "success";

    }

}
